package org.dragonnova.business.service.impl;

import java.util.List;

import org.dragonnova.business.chdao.WxAdminDao;
import org.dragonnova.business.dao.UserDao;
import org.dragonnova.business.model.User;
import org.dragonnova.business.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.base.pub.util.Base64Util;

@Service("userService")
public class UserServiceImpl extends BaseServiceImpl implements UserService {
    @Autowired
    private UserDao userDao;
    @Autowired
    private WxAdminDao wxAdminDao;

    @Override
    public User getUserByName(String userName) throws Exception {
        return userDao.findUserByName(userName);
    }

    @Override
    public void insertUser(User user) throws Exception {
        userDao.insertUser(user);
    }

    @Override
    public boolean updateUser(User user) throws Exception {
        return userDao.updateUser(user) > 0 ? true : false;
    }

    @Override
    public List<User> findUser(String id, String searchText) throws Exception {
        if (searchText != null && searchText.length() > 0) {
            searchText = "%" + searchText + "%";
        }
        return wxAdminDao.findUser(id, searchText);
    }

    @Override
    public boolean delUser(Integer id) throws Exception {
        int result = userDao.delUser(id);
        return result > 0 ? true : false;
    }

    @Override
    public boolean addUser(User user) throws Exception {
        int result = userDao.addUser(user);
        return result > 0;
    }

    @Override
    public List<User> findWxAdmin(String searchStr) throws Exception {
        return wxAdminDao.findWxAdmin(searchStr);
    }

    @Override
    public User findAdminByName(String userName) throws Exception {
        return wxAdminDao.findAdminByName(userName);
    }

    @Override
    public boolean updateAdmin(User user) throws Exception {
        String id = user.getId();
        user.setIdFromBase64(id);
        if (id == null || id.equals("null")) {
            int result = wxAdminDao.insertWxUser(user);
            return result > 0;
        } else {
            int result = wxAdminDao.updateAdmin(user);
            return result > 0;
        }
    }

    public boolean updateAdminIP(String id, String IP) throws Exception {
        int result = wxAdminDao.updateAdminIP(id,IP);
        return result > 0;
    }
}
